package com.hqyj.web.listener;

import com.hqyj.pojo.User;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @author cz
 * 2021/8/16 9:35
 */
@WebListener
public class HttpSessionLifeCycleListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        //获取servletContext对象，然后将所对应的HttpSession对象销毁
        HttpSession session = httpSessionEvent.getSession();
        ServletContext context = session.getServletContext();
        User user = (User) session.getAttribute("user");
        context.removeAttribute(user.getId()+"");
    }
}
